//package com.bestcem.xm.user.grpc.client;
//
//import com.bestcem.bp.xm.user.grpc.v1.services.CheckAuthRequest;
//import com.bestcem.bp.xm.user.grpc.v1.services.CheckAuthResponse;
//import com.bestcem.bp.xm.user.grpc.v1.services.Token;
//import com.bestcem.bp.xm.user.grpc.v1.services.UserAuthServiceGrpc;
//import com.bestcem.xm.common.core.constant.AppRequestErrorCode;
//import com.bestcem.xm.common.core.constant.GrpcClientNames;
//import com.bestcem.xm.common.core.domain.web.ServiceResult;
//import com.bestcem.xm.common.core.enums.AuthErrorCodeEnum;
//import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
//import com.bestcem.xm.component.security.dto.TokenDTO;
//import com.bestcem.xm.component.security.dto.AuthenticationDTO;
//import com.bestcem.xm.user.util.business.UserJwtTokenUtil;
//import io.grpc.Status;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.client.inject.GrpcClient;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//
///**
// * @author Linked <zhijian.lin@idiaoyan.com>
// * @version v1.0
// * @date 2022/6/7 19:29
// */
//@Slf4j
//@Service
//public class UserAuthGrpcClientService {
//
//    @Resource
//    private UserJwtTokenUtil userJwtTokenUtil;
//
//    @GrpcClient(GrpcClientNames.DS_XM)
//    private UserAuthServiceGrpc.UserAuthServiceBlockingStub stub;
//
//
//    public ServiceResult<AuthenticationDTO> checkAuthentication(String token, Boolean authentication, String uri,
//                                                                String method) {
//        // 判断是否登出
//        boolean exist = userJwtTokenUtil.existJwtInvalid(token);
//        if (exist) {
//            return ServiceResult.fail(ReqErrorCodeEnum.INVALID_TOKEN, "Logged out token");
//        }
//
//        CheckAuthResponse response = stub.checkAuth(CheckAuthRequest.newBuilder()
//                .setToken(token).setAuthentication(authentication).setUri(uri).setMethod(method).build());
//        if (response.getStatus().getCodeValue() != Status.Code.OK.value()) {
//            log.error("auth failed, unknown error, token: {}, authentication: {}, uri: {}, method: {}, response: {}",
//                    token, authentication, uri, method, response);
//            // 对超时做特殊处理
//            if (String.valueOf(AuthErrorCodeEnum.EXPIRE_TOKEN.getMsg()).equals(response.getStatus().getMessage())) {
//                return ServiceResult.fail(AuthErrorCodeEnum.EXPIRE_TOKEN, AuthErrorCodeEnum.EXPIRE_TOKEN.getMsg());
//            } else if (String.valueOf(ReqErrorCodeEnum.INVALID_TOKEN.getMsg()).equals(response.getStatus().getMessage())) {
//                ServiceResult<AuthenticationDTO> serviceResult = ServiceResult.fail("Invalid Token");
//                serviceResult.setErrorCode(String.valueOf(AppRequestErrorCode.INVALID_TOKEN));
//                return serviceResult;
//            }
//            return ServiceResult.fail("鉴权失败");
//        }
//
//        AuthenticationDTO dto = new AuthenticationDTO();
//        dto.setPermission(response.getPermission());
//        dto.setToken(this.toTokenDTO(response.getToken()));
//        return ServiceResult.success(dto);
//
//    }
//
//    /**
//     * 类型转换
//     *
//     * @param token
//     * @return
//     */
//    private TokenDTO toTokenDTO(Token token) {
//        TokenDTO dto = new TokenDTO();
//        dto.setAvatar(token.getAvatar());
//        dto.setOrgCode(token.getOrgCode());
//        dto.setOrgId(token.getOrgId());
//        dto.setRealName(token.getName());
//        dto.setSuperRole(token.getSuper() ? 1 : 0);
//        dto.setUserId(token.getUserId());
//        return dto;
//    }
//}
